package com.eric.helper;


import com.eric.dos.SchoolDO;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: DO转表
 * @author: liuBing
 * @create: 2024-01-29 16:33
 **/
public class DOToTableHelper {


    public static void main(String[] args) {
        System.out.println(generateCreateTableSQL(SchoolDO.class));
    }

    @SneakyThrows
    public static String generateCreateTableSQL(Class<?> entityClass) {
        StringBuilder sqlBuilder = new StringBuilder();

        // 构建 CREATE TABLE 语句
        sqlBuilder.append("CREATE TABLE ")
                .append(camelCaseToUnderscore(entityClass.getSimpleName()))
                .append(" (");

        // 获取实体类的所有字段
        Field[] fields = entityClass.getDeclaredFields();

        // 存储字段的 SQL 语句
        List<String> columnSQLList = new ArrayList<>();

        columnSQLList.add("id BIGINT auto_increment primary key");

        // 遍历字段
        for (Field field : fields) {
            // 获取字段名
            String fieldName = field.getName();
            if("id".equals(fieldName) || "serialVersionUID".equals(fieldName) ){
                continue;
            }
            String columnName = camelCaseToUnderscore(fieldName);

            Class<?> fieldType = field.getType();
            String columnType;
            // 添加字段的 SQL 语句
            if (fieldType.equals(String.class)) {
                columnType = " VARCHAR(255)";
            } else if (fieldType.equals(Integer.class)) {
                columnType = " INT";
            } else if (fieldType.equals(Date.class)) {
                columnType = " DATETIME";
            } else if (fieldType.equals(Long.class)) {
                columnType = " BIGINT";
            } else if (fieldType.equals(Double.class)) {
                columnType = " DOUBLE";
            } else if (fieldType.equals(Boolean.class)) {
                columnType = " TINYINT";
            } else {
                columnType = " VARCHAR(255)";
            }

            columnSQLList.add(columnName + columnType + " null");
        }

        // 将所有字段的 SQL 语句连接成字符串
        String columnsSQL = String.join(", ", columnSQLList);
        // 将字段 SQL 添加到 CREATE TABLE 语句中
        sqlBuilder.append(columnsSQL);
        // 结尾
        sqlBuilder.append(");");
        return sqlBuilder.toString();
    }


    public static String camelCaseToUnderscore(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        if (input.endsWith("DO")){
            input = input.substring(0, input.length() - 2);
        }

        StringBuilder sb = new StringBuilder(input.length());

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (Character.isUpperCase(c)) {
                // 如果当前字符是大写，并且不是第一个字符，则在前面添加下划线
                if (i > 0) {
                    sb.append('_');
                }
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

}
